﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.MountAndBlade.View.Tableaus.BrightnessDemoTableau
// Assembly: TaleWorlds.MountAndBlade.View, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 61152EB8-513B-4D4A-8AC0-0D1465C6CF6C
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\Modules\Native\bin\Win64_Shipping_Client\TaleWorlds.MountAndBlade.View.dll

using System;
using TaleWorlds.DotNet;
using TaleWorlds.Engine;
using TaleWorlds.Engine.Options;
using TaleWorlds.Library;

#nullable disable
namespace TaleWorlds.MountAndBlade.View.Tableaus
{
  public class BrightnessDemoTableau
  {
    private MatrixFrame _frame;
    private Scene _tableauScene;
    private Texture _demoTexture;
    private Camera _continuousRenderCamera;
    private bool _initialized;
    private int _tableauSizeX;
    private int _tableauSizeY;
    private int _demoType = -1;
    private bool _isEnabled;
    private float RenderScale = 1f;

    public Texture Texture { get; private set; }

    private TableauView View
    {
      get
      {
        return (NativeObject) this.Texture != (NativeObject) null ? this.Texture.TableauView : (TableauView) null;
      }
    }

    private void SetEnabled(bool enabled)
    {
      this._isEnabled = enabled;
      TableauView view = this.View;
      if (!this._initialized)
        this.SetScene();
      view?.SetEnable(this._isEnabled);
    }

    public void SetDemoType(int demoType)
    {
      this._demoType = demoType;
      this._initialized = false;
      this.RefreshDemoTableau();
    }

    public void SetTargetSize(int width, int height)
    {
      int num1;
      int num2;
      if (width <= 0 || height <= 0)
      {
        num1 = 10;
        num2 = 10;
      }
      else
      {
        this.RenderScale = NativeOptions.GetConfig(NativeOptions.NativeOptionsType.ResolutionScale) / 100f;
        num1 = (int) ((double) width * (double) this.RenderScale);
        num2 = (int) ((double) height * (double) this.RenderScale);
      }
      if (num1 == this._tableauSizeX && num2 == this._tableauSizeY)
        return;
      this._tableauSizeX = num1;
      this._tableauSizeY = num2;
      this.View?.SetEnable(false);
      this.View?.AddClearTask(true);
      this.Texture?.ReleaseNextFrame();
      this.Texture = TableauView.AddTableau("BrightnessDemo", new RenderTargetComponent.TextureUpdateEventHandler(this.SceneTableauContinuousRenderFunction), (object) this._tableauScene, this._tableauSizeX, this._tableauSizeY);
    }

    public void OnFinalize()
    {
      if ((NativeObject) this._continuousRenderCamera != (NativeObject) null)
      {
        this._continuousRenderCamera.ReleaseCameraEntity();
        this._continuousRenderCamera = (Camera) null;
      }
      this.View?.SetEnable(false);
      this.View?.AddClearTask();
      this.Texture = (Texture) null;
      this._tableauScene = (Scene) null;
    }

    public void SetScene()
    {
      this._tableauScene = Scene.CreateNewScene();
      switch (this._demoType)
      {
        case 0:
          this._demoTexture = Texture.GetFromResource("brightness_calibration_wide");
          this._tableauScene.SetAtmosphereWithName("brightness_calibration_screen");
          break;
        case 1:
          this._demoTexture = Texture.GetFromResource("calibration_image_1");
          this._tableauScene.SetAtmosphereWithName("TOD_11_00_SemiCloudy");
          break;
        case 2:
          this._demoTexture = Texture.GetFromResource("calibration_image_2");
          this._tableauScene.SetAtmosphereWithName("TOD_05_00_SemiCloudy");
          break;
        case 3:
          this._demoTexture = Texture.GetFromResource("calibration_image_3");
          this._tableauScene.SetAtmosphereWithName("exposure_calibration_interior");
          break;
        default:
          Debug.FailedAssert(string.Format("Undefined Brightness demo type({0})", (object) this._demoType), "C:\\Develop\\MB3\\Source\\Bannerlord\\TaleWorlds.MountAndBlade.View\\Tableaus\\BrightnessDemoTableau.cs", nameof (SetScene), 130);
          break;
      }
      this._tableauScene.SetDepthOfFieldParameters(0.0f, 0.0f, false);
    }

    private void RefreshDemoTableau()
    {
      if (this._initialized)
        return;
      this.SetEnabled(true);
    }

    public void OnTick(float dt)
    {
      if ((NativeObject) this._continuousRenderCamera == (NativeObject) null)
        this._continuousRenderCamera = Camera.CreateCamera();
      this.View?.SetDoNotRenderThisFrame(false);
    }

    internal void SceneTableauContinuousRenderFunction(Texture sender, EventArgs e)
    {
      Scene userData = (Scene) sender.UserData;
      TableauView tableauView = sender.TableauView;
      tableauView.SetEnable(true);
      if ((NativeObject) userData == (NativeObject) null)
      {
        tableauView.SetContinuousRendering(false);
        tableauView.SetDeleteAfterRendering(true);
      }
      else
      {
        userData.SetShadow(false);
        userData.EnsurePostfxSystem();
        userData.SetDofMode(false);
        userData.SetMotionBlurMode(false);
        userData.SetBloom(true);
        userData.SetDynamicShadowmapCascadesRadiusMultiplier(0.31f);
        userData.SetExternalInjectionTexture(this._demoTexture);
        tableauView.SetRenderWithPostfx(true);
        if (!((NativeObject) this._continuousRenderCamera != (NativeObject) null))
          return;
        Camera continuousRenderCamera = this._continuousRenderCamera;
        tableauView.SetCamera(continuousRenderCamera);
        tableauView.SetScene(userData);
        tableauView.SetSceneUsesSkybox(false);
        tableauView.SetDeleteAfterRendering(false);
        tableauView.SetContinuousRendering(true);
        tableauView.SetDoNotRenderThisFrame(true);
        tableauView.SetClearColor(4278190080U);
        tableauView.SetFocusedShadowmap(true, ref this._frame.origin, 1.55f);
      }
    }
  }
}
